﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;

namespace MII_FIF.Layouts.MII_FIF
{
    public partial class ViewWorkOrder : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.WorkOrderGroupName))
            {
                ((Label)Header.FindControl("lblHeader")).Text = "Vendor LogBook - Work Order";
                btnDelete.Click += btnDelete_Click;
                GridDataTable.RowCommand += GridDataTable_RowCommand;
                btnAdd.Click += btnAdd_Click;
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/VendorLogBook/FormWorkOrder.aspx"));
        }

        void GridDataTable_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/VendorLogBook/FormWorkOrder.aspx?ID=" + e.CommandArgument));
            }
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool delete = false;
            DataTable datatable = (DataTable)ViewState["datatable"];
            InventoryManagement.Work_Order workOrder = new InventoryManagement.Work_Order();
            int stat = 0;
            foreach (GridViewRow row in GridDataTable.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chk");
                if (chk != null)
                    if (chk.Checked)
                    {
                        delete = true;
                        break;
                    }
            }

            if (delete)
            {
                for (int i = GridDataTable.Rows.Count - 1; i > -1; i--)
                {
                    CheckBox chk = (CheckBox)GridDataTable.Rows[i].FindControl("chk");
                    if (chk != null)
                        if (chk.Checked)
                        {                                                        
                            workOrder.ID = Guid.Parse(datatable.Rows[i]["ID"].ToString());
                            stat = workOrder.Delete();
                            datatable.Rows.RemoveAt(i);
                            if (stat < 0)
                            {
                                lblError.Text += "Delete Failed"; break;
                            }
                        }
                }
                GridDataTable.DataSource = datatable;
                GridDataTable.DataBind();
                ViewState["datatable"] = datatable;
            }
            else
            {
                lblError.Text += "Please check at least 1 item to delete <br />";
            }
        }

        void load()
        {
            InventoryManagement.Work_Order workOrder = new InventoryManagement.Work_Order();
            DataTable datatable = workOrder.SelectByParam();
            datatable.Columns.Add("LabelName");
            foreach (DataRow row in datatable.Rows)
            {
                InventoryManagement.Inventory_Physical physical = new InventoryManagement.Inventory_Physical();
                physical.ID = Guid.Parse(row["ID_LabelName"].ToString());
                physical.Select();
                row["LabelName"] = physical.LabelName;
            }
            GridDataTable.DataSource = datatable;
            ViewState["datatable"] = datatable;
            GridDataTable.DataBind();
        }
    }
}
